<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>添加用户信息</title>
<script type="text/javascript" src="javascript/jquery-3.2.1.js"></script>
<script type="text/javascript" src="javascript/window/openwindow.js"></script>
  <script>
    function submit(){
    	var pass = validate();
    	if (pass == true) {
    		document.getElementById("form").submit();
    	} else {
    		return;
    	}
    }
    //表单校验
    function validate() {
    	if ($('#account').val() == "") {
    		mmy_alert("账号不能为空！");
    		return false;
    	} else {
    		var text = "";
    		var account = document.getElementById("account").value;
    		var data = "account=" + account;
            data += "&id=${user1.id }";
    		$.ajax({
    			type: "post",
    		    url: "UserServlet?action=selectAccount",
    		    data: data,
    		    async: false,
    		    success: function(responseText) {
    		    	text = responseText;
    		    }
    		});
    		if (text != "") {
    			mmy_alert(text);
                return false;
    		}
    		/*
    		
            //构造数据
            
            //创建xhr对象 
            var xhr = new XMLHttpRequest();
            //设置xhr请求的超时时间
            //xhr.timeout = 3000;
            //设置响应返回的数据格式
            xhr.responseType = "text";
            //创建一个 post 请求，采用异步
            xhr.open('POST', 'UserServlet?action=selectAccount', false);
            //注册相关事件回调处理函数
            xhr.onload = function(e) { 
            	if (this.responseText != "") {
            		mmy_alert(this.responseText);
                    return false;
            	}
            };
            xhr.ontimeout = function(e) { alert(1); };
            xhr.onerror = function(e) { alert(2); };
            //xhr.upload.onprogress = function(e) { alert(3); };
            //发送数据
            xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
            xhr.setRequestHeader("cache-control", "no-cache");
            xhr.send(data);
            */
    	}
    	if ($('#userPassword').val() == "" && $('#password').val() == "") {
            mmy_alert("密码不能为空！");
            return false;
        }
    	if ($('#name').val() == "") {
            mmy_alert("姓名不能为空！");
            return false;
        }
    	var password = document.getElementById("password").value;
        var password1 = document.getElementById("password1").value;
        if (password != password1) {
            document.getElementById("warn").style.display = "inline";
            return false;
        } else {
            document.getElementById("warn").style.display = "none";
            if (password != "") {
                document.getElementById("userPassword").value = password;
            }
        }
        return true;
    }
    window.onload = function () {
    	//document.getElementById("password").value="password";
    	var userId = "${user1.id }";
    	if (userId != "") {
    		document.getElementById("password").placeholder="*********";
    		document.getElementById("password1").placeholder="*********";
    	}
    	
    	var sex = "${user1.sex }";
    	if (sex == "女") {
    		document.getElementById("sex").selected = "selected";
    	}
    }
    
  </script>
  <style type="text/css">
  </style>
</head>
<body>
  <form id="form" action="UserServlet?action=saveUser" method="post">
    <input type="hidden" name="id" value="${user1.id }">
    <input id="userPassword" type="hidden" name="password" value="${user1.password }">
	<table id="table">
	  <tr><td>账号：</td><td><input id="account" type="text" name="account" value="${user1.account }"></td></tr>
	  <tr><td>密码：</td><td><input id="password" type="password" placeholder="" value=""></td></tr>
	  <tr><td>确认密码：</td><td><input id="password1" type="password" value=""  placeholder=""><a id="warn" style="display:none;color:red;"> 请再次确认密码！</a></td></tr>
	  <tr><td>姓名：</td><td><input id="name" type="text" name="name" value="${user1.name }"></td></tr>
	  <tr>
	    <td>性别：</td>
	    <td>
	      <select name="sex">
	        <option value="男">男</option>
	        <option id="sex" value="女">女</option>
	      </select>
	    </td>
	  </tr>
	  <tr>
	    <td>角色：</td>
	    <td>
	      <select name="role">
	        <c:forEach var="role2" items="${roleList}">
              <option value="${role2.id}" ${role2.selected}>${role2.role}</option>
            </c:forEach>
	      </select>
	    </td>
	  </tr>
	</table>
	<!-- <a onclick="addContinue">继续添加</a><br> -->
	<input type="hidden" name="role" value="${role}"/>
	<a href="javascript:submit();">确认</a>
	<a href="javascript:history.go(-1)">返回</a>
  </form>
</body>
</html>